package user;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "GetUserInfoServlet")
public class GetUserInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        String userId = request.getParameter("user_id");

        String email = UserSession.getUserSessionEmail(session);

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");


        PrintWriter out = response.getWriter();

        ResultSet userSet = null;
        boolean isUserIdEmpty = false;
        boolean getMyInfo = false;
        if (userId == null || userId.isEmpty()){
            isUserIdEmpty = true;
        }

        if (email == null || email.isEmpty()){
            if (isUserIdEmpty){
                out.println(JsonOut.error(220, "not_login"));
                return;
            }

            // 查询userId的资料
            userSet = (new MysqlUtils()).getUserIdInfo(userId);
        }else {
            if (isUserIdEmpty){
                getMyInfo = true;
                userSet = (new MysqlUtils()).getUserInfo(email);
            }else {
                String myId = (new MysqlUtils()).getUserId(email);

                if (myId != null && !myId.isEmpty()){
                    if (myId.equals(userId)) {
                        getMyInfo = true;
                    }
                }
                if (getMyInfo){
                    userSet = (new MysqlUtils()).getUserInfo(email);
                }else {
                    userSet = (new MysqlUtils()).getUserIdInfo(userId);
                }
            }
        }


        if (userSet == null){
            out.println(JsonOut.error(101, "not_exist"));
            return;
        }

        //nickname,signature,area,sex,birthday,head_image,follow_num,fans_num,email_active(id)
        Map<String, Object> userInfoJObject = new HashMap<>();
        try {
            String nickname = userSet.getString(1);
            String signature = userSet.getString(2);
            String area = userSet.getString(3);
            String sex = userSet.getString(4);
            String birthday = userSet.getString(5);
            String headImage = userSet.getString(6);
            String followNum = userSet.getString(7);
            String fansNum = userSet.getString(8);
            userInfoJObject.put("nickname", nickname != null ? nickname : "");
            userInfoJObject.put("signature", signature != null ? signature : "");
            userInfoJObject.put("area", area != null ? area : "");
            userInfoJObject.put("sex", sex != null ? sex : "");
            userInfoJObject.put("birthday", birthday != null ? birthday : "");
            userInfoJObject.put("head_image", headImage != null ? headImage : "");
            userInfoJObject.put("follow_num", followNum != null ? followNum : "");
            userInfoJObject.put("fans_num", fansNum != null ? fansNum : "");

            String diffColumn = userSet.getString(8);
            if (getMyInfo){
                userInfoJObject.put("email_active", diffColumn != null ? diffColumn : "");
                userInfoJObject.put("follow_state", "self");
            }else {
                userInfoJObject.put("user_id", diffColumn != null ? diffColumn : "");
                if (email != null && !email.isEmpty()){
                    String followState = (new MysqlUtils()).getFollowStatus(email, userId);
                    userInfoJObject.put("follow_state", followState != null ? followState : "");
                }
//                else {
//                    userInfoJObject.put("follow_state", "stranger");
//                }


            }

//            System.out.println(userInfoJObject);

            out.println(JsonOut.common(0, "user_info", userInfoJObject));


        }catch (Exception e){
            e.printStackTrace();
            out.println(JsonOut.error(500, "error"));
        }
    }
}
